package com.pl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pl.entity.WmStorageLocation;
import com.pl.service.WmStorageLocationService;
import com.pl.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 库区表(WmStorageLocation)表控制层
 *
 * @author makejava
 * @since 2024-04-28 09:34:57
 */
@RestController
@RequestMapping("/api/wmWarehouseLocation")
public class WmStorageLocationController {
    /**
     * 服务对象
     */
    @Autowired
    private WmStorageLocationService wmStorageLocationService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list/page")
    public R queryByPage(@RequestParam Map<String, Object> map) {
        int pageNum = Integer.valueOf((String) map.get("pageNum"));
        int pageSize = Integer.valueOf((String) map.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);
        List<WmStorageLocation> wmStorageLocations = wmStorageLocationService.queryByPage(map);
        if (wmStorageLocations != null) {
            PageInfo<WmStorageLocation> wmStorageLocationPageInfo = new PageInfo<>(wmStorageLocations);
            return R.ok(wmStorageLocationPageInfo);
        }
        return R.fail("查询失败");


    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok(this.wmStorageLocationService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param wmStorageLocation 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(WmStorageLocation wmStorageLocation, HttpSession session) {
        boolean insert = this.wmStorageLocationService.insert(wmStorageLocation, session);
        if (insert) {
            return R.ok();
        }
        return R.fail("新增失败！");
    }

    /**
     * 编辑数据
     *
     * @param wmStorageLocation 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(WmStorageLocation wmStorageLocation) {
        boolean update = this.wmStorageLocationService.update(wmStorageLocation);
        if (update) {
            return R.ok();
        }
        return R.fail("更新失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") int id) {
        return R.ok(this.wmStorageLocationService.deleteById(id));
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer> list){
        boolean result=wmStorageLocationService.deleteBatch(list);
        if (result){
            return R.ok();
        }
        return R.fail("批量删除失败");
    }
    /**
     * 上传文件（需要监听器，批量增加）
     * @param file
     * @return
     */
    @RequestMapping("/importwmWarehouseLocationFile")
    public String importwmWarehouseLocationFile(@Param("file") MultipartFile file){
        return wmStorageLocationService.importwmWarehouseLocationFile(file);
    }

    /**
     * 导出文件
     * @return
     */
    @RequestMapping("/exportwmWarehouseLocationFile")
    public void exportwmWarehouseLocationFile(HttpServletResponse response){
        wmStorageLocationService.exportwmWarehouseLocationFile(response);
    }
}

